package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.bean.DeviceBean;
import com.mercenary.wein.screenControl.command.CommandTool;
import com.mercenary.wein.screenControl.ui.util.Constants;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.border.Border;

public class MassMessage extends JDialog {
    // Log log = LogFactory.getLog(ImportAddressBook.class);
    GroupCheckBoxPanel RequestJ1 = new GroupCheckBoxPanel();//面板1
    JPanel RequestJ2 = new JPanel();//面板2
    static JFrame locationjf = new JFrame();

    public MassMessage() {
        super(locationjf, "群发消息");
        setBounds(200, 100, 700, 550);
        setLayout(new BorderLayout());
        setVisible(true);
        add(RequestJ1, BorderLayout.WEST);
        add(RequestJ2, BorderLayout.EAST);
        RequestJ1.setPreferredSize(new Dimension(180, 550));//设置大小
        RequestJ2.setLayout(new FlowLayout(FlowLayout.LEADING, 500, 10));
        RequestJ1.setBackground(Color.WHITE);
        RequestJ2.setPreferredSize(new Dimension(480, 550));
        RequestJ2.setBackground(Color.WHITE);
        Border titleBorder1 = BorderFactory.createTitledBorder("请选择在线设备");// 设置面板边框，实现分组框的效果       .设置面板边框颜色
        RequestJ1.setBorder(titleBorder1);
        //J2中的组件
        FlowLayout layout = new FlowLayout(FlowLayout.LEADING);
        RequestJ2.setLayout(layout);
        JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel jp3 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        final JPanel jp4 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        final ExeModeButtonGroup jp5 = new ExeModeButtonGroup(this);
        JPanel jp6 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jp4.setLayout(new BorderLayout());
        final JPanel jptime = new JPanel();
        jp1.setPreferredSize(new Dimension(500, 30));//设置大小
        jp2.setPreferredSize(new Dimension(500, 30));//设置大小
        jp3.setPreferredSize(new Dimension(500, 30));//设置大小
        jp4.setPreferredSize(new Dimension(500, 180));//设置大小
        jp5.setPreferredSize(new Dimension(500, 150));//设置大小
        jp6.setPreferredSize(new Dimension(500, 30));//设置大小
        JLabel L1 = new JLabel("发送对象:");
        JLabel L2 = new JLabel("发送人/群组数:");
        JLabel L3 = new JLabel("(-1表示全部)");
        JLabel L4 = new JLabel("~");
        JLabel L5 = new JLabel("秒");
        JLabel L6 = new JLabel("消息类型：");
        final JTextField num = new JTextField();
        num.setPreferredSize(new Dimension(35, 25));
        final JTextField start = new JTextField("3");
        start.setPreferredSize(new Dimension(35, 25));
        final JTextField end = new JTextField("5");
        end.setPreferredSize(new Dimension(35, 25));
        final JCheckBox j3 = new JCheckBox("间隔");
        final JRadioButton jrba = new JRadioButton("微信好友");
        jrba.setSelected(true);
        final JRadioButton jrbb = new JRadioButton("群聊组");
        ButtonGroup selectgroup = new ButtonGroup();// 创建单选按钮组
        selectgroup.add(jrba);// 将radioButton1增加到单选按钮组中
        selectgroup.add(jrbb);// 将radioButton2增加到单选按钮组中
        final JRadioButton jrbc = new JRadioButton("文本消息");
        jrbc.setSelected(true);
        final JRadioButton jrbd = new JRadioButton("图片消息");
        ButtonGroup messagegroup = new ButtonGroup();// 创建单选按钮组
        messagegroup.add(jrbc);// 将radioButton1增加到单选按钮组中
        messagegroup.add(jrbd);// 将radioButton2增加到单选按钮组中
        JRadioButton JRB1 = new JRadioButton("立即执行");
        JRadioButton JRB2 = new JRadioButton("定时执行");
        JRadioButton JRB3 = new JRadioButton("循环执行");
        ButtonGroup excutegroup = new ButtonGroup();// 创建单选按钮组
        excutegroup.add(JRB1);// 将radioButton1增加到单选按钮组中
        excutegroup.add(JRB2);// 将radioButton2增加到单选按钮组中
        excutegroup.add(JRB3);// 将radioButton2增加到单选按钮组中
        jp1.add(L1);
        jp1.add(jrba);
        jp1.add(jrbb);
        jp2.add(L2);
        jp2.add(num);
        jp2.add(L3);
        jp3.add(j3);
        jp3.add(start);
        jp3.add(L4);
        jp3.add(end);
        jp3.add(L5);
        JPanel jmessage = new JPanel(new FlowLayout(FlowLayout.LEADING));
        jmessage.add(L6);
        jmessage.add(jrbc);
        jmessage.add(jrbd);
        jp4.add(jmessage, BorderLayout.NORTH);

			/*选择图片或文本diamante*/


        JButton submit = new JButton("提交任务");
        //@涂笑
        //submit.addActionListener(new SubmitListener(J1.getSelectedDevices(),phoneNums,importMode));
        jp6.add(submit);
        RequestJ2.add(jp1);
        RequestJ2.add(jp2);
        RequestJ2.add(jp3);
        RequestJ2.add(jp4);
        RequestJ2.add(jp5);
        RequestJ2.add(jp6);


        //发送对象的选择按钮点击事件

        final JPanel Jmessagepanel = new JPanel(new BorderLayout());
        final JTextArea messagearea = new JTextArea(5, 10);

        jrbc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Jmessagepanel.removeAll();
                Jmessagepanel.repaint();
                JLabel Jtip = new JLabel("文字消息：(100字以内)");
                Jmessagepanel.add(Jtip, BorderLayout.NORTH);
                Jmessagepanel.add(messagearea, BorderLayout.SOUTH);
                jp4.add(Jmessagepanel, BorderLayout.SOUTH);
                jp4.validate();

            }
        });
        final List<File> choosedFile = new ArrayList<File>();
        jrbd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Jmessagepanel.removeAll();
                Jmessagepanel.repaint();
                //JPanel Jmessagepanel=new JPanel();
                Jmessagepanel.setBackground(Color.BLACK);
                JButton Jadd = new JButton("添加本地图片（最多支持9张图片）");
                Jmessagepanel.setPreferredSize(new Dimension(100, 120));
                //Jadd.setBounds(100,200,30,30);
                jp4.add(Jadd, BorderLayout.CENTER);
                jp4.add(Jmessagepanel, BorderLayout.SOUTH);
                jp4.validate();
                Jadd.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser fileChooser = new JFileChooser(Constants.RepertoryPath);// 以c:\\winnt为打开文件为对话框的默认路径
                        int result = fileChooser.showOpenDialog(locationjf);
                        // /如果选择确定按钮的话，那么则可以打开现有的文件
                        if (result == JFileChooser.APPROVE_OPTION) {
                            File file = fileChooser.getSelectedFile();
                            choosedFile.add(file);
                        }
                        // /否则如果单击取消的话，那么就会在标签中显示"你没有选取文件"
                        else if (result == fileChooser.CANCEL_OPTION) {
                            choosedFile.removeAll(choosedFile);
                        }
                    }
                });
            }
        });

        submit.addActionListener(new ActionListener() {
            int flag = 0;
            int secondflag = 0;

            public void actionPerformed(ActionEvent e) {
                if (jrba.isSelected()) {
                    flag = 1;
                } else if (jrbb.isSelected()) {
                    flag = 2;
                }
                if (jrbc.isSelected()) {
                    secondflag = 1;
                } else if (jrbd.isSelected()) {
                    secondflag = 2;
                }

                CommandTool.massMessage(RequestJ1.getSelectedDevices(),
                        messagearea.getText(),choosedFile,jp5.getExeModeBean());

            }
        });
        RequestJ2.validate();
    }

    public static void main(String[] arge) {
        new MassMessage();


    }

}


